package com.zhudoo.zhilian.api;

import com.zhudoo.zhilian.service.ChatService;
import com.zhudoo.zhilian.util.web.Response;
import com.zhudoo.zhilian.util.web.ResponseUtil;
import com.zhudoo.zhilian.util.web.ServiceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author zhudao
 */
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    ChatService chatService;

    @RequestMapping(value = "/getChatList", method = RequestMethod.GET)
    public Response getChatList(@RequestHeader String token) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        List<Map<String, Object>> resultList = chatService.getChatList(serviceContext);
        return ResponseUtil.responseSuccess("获取成功", resultList);
    }

    @RequestMapping(value = "/getChatRecord", method = RequestMethod.GET)
    public Response getChatRecord(@RequestHeader String token, Integer toUserId, Integer tipId) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("toUserId", toUserId);
        serviceContext.addParams("tipId", tipId);
        List<Map<String, Object>> resultList = chatService.getChatRecord(serviceContext);
        return ResponseUtil.responseSuccess("获取成功", resultList);
    }

    @RequestMapping(value = "/deleteChat", method = RequestMethod.POST)
    public Response deleteChat(@RequestHeader String token, Integer tipId, Integer toUserId) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("tipId", tipId);
        serviceContext.addParams("toUserId", toUserId);
        chatService.deleteChat(serviceContext);
        return ResponseUtil.responseSuccess("删除成功");
    }

    @RequestMapping(value = "/haveRead", method = RequestMethod.POST)
    public Response haveRead(@RequestHeader String token, Integer tipId, Integer toUserId) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("tipId", tipId);
        serviceContext.addParams("toUserId", toUserId);
        chatService.haveRead(serviceContext);
        return ResponseUtil.responseSuccess("已读成功");
    }

    @RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
    public Response sendMessage(@RequestHeader String token,
                                Integer tipId, Integer toUserId,
                                Integer contentType, String content,
                                Double latitude, Double longitude,
                                String address, String name) {
        ServiceContext serviceContext = ServiceContext.getInstance();
        serviceContext.addParams("token", token);
        serviceContext.addParams("tipId", tipId);
        serviceContext.addParams("toUserId", toUserId);
        serviceContext.addParams("contentType", contentType);
        serviceContext.addParams("content", content);
        serviceContext.addParams("latitude", latitude);
        serviceContext.addParams("longitude", longitude);
        serviceContext.addParams("address", address);
        serviceContext.addParams("name", name);
        chatService.sendMessage(serviceContext);
        return ResponseUtil.responseSuccess("发送成功");
    }


}
